package com.cwnuzj.ffms.ui.view.navigation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;

import com.cwnuzj.ffms.db.DbUtilsTemplate;
import com.cwnuzj.ffms.db.JdbcUtil;
import com.cwnuzj.ffms.model.User;
import com.cwnuzj.ffms.model.navigation.Category;
import com.cwnuzj.ffms.model.navigation.NavTreeData;
import com.cwnuzj.ffms.model.navigation.NavTreeData.ActionEditorData;
import com.cwnuzj.ffms.model.navigation.TreeNodeObject;
import com.cwnuzj.ffms.util.RCPUtil;

/**
 * 功能导航视图类
 * 
 * @author Jeff Zheng
 */
public class NavigationView extends ViewPart {
	public static final String ID = "com.cwnuzj.ffms.navigation";
	private TreeViewer treeViewer;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		treeViewer.setContentProvider(new NavViewContentProvider());
		treeViewer.setLabelProvider(new NavViewLabelProvider());

		// 展开整棵树
		treeViewer.setAutoExpandLevel(2);
		// Provide the input to the ContentProvider
		treeViewer.setInput(new NavTreeData());
		getSite().setSelectionProvider(treeViewer);

		// add double click listener
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
				StructuredSelection select = (StructuredSelection) event.getSelection();
				if (select.getFirstElement() instanceof Category) {
					Category category = (Category) select.getFirstElement();
					if (treeViewer.getExpandedState(category)) {
						// 若为顶层节点,则折叠树
						treeViewer.collapseToLevel(category, 1);
					} else {
						treeViewer.expandToLevel(category, 1);
					}
				} else {// 如果是子节点，则激活或打开相应的编辑器
					Map<String, ActionEditorData> editorsMap = ((NavTreeData) treeViewer.getInput()).getEditorsMap();
					ActionEditorData actionEditorData = editorsMap.get(((TreeNodeObject) select.getFirstElement()).getName());
					
					RCPUtil.openEditor(handlerService,actionEditorData.getEditorId(),actionEditorData.getCommandId());
				}
			}
		});

		try {
			DbUtilsTemplate dbUtilsTemplate = new DbUtilsTemplate(JdbcUtil.getDataSource());
			List<User> userList = dbUtilsTemplate.find(User.class, "SELECT * from ffms.t_user");
			for (User user : userList) {
				System.out.println("UserName: " + user.getUsername());
				System.out.println("Password: " + user.getPassword());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
}